package client;

import com.esotericsoftware.minlog.Log;
import java.awt.MouseInfo;

public class IdleManager extends Thread {

    private ClientManager client;
    private boolean running;

    public IdleManager(ClientManager clientManager) {
        this.client = clientManager;
        running = false;
    }
    
    public boolean isRunning() {
        return running;
    }

    @Override
    public void run() {
        running = true;
        
        while (!Thread.interrupted()) {
            int xOld = MouseInfo.getPointerInfo().getLocation().x;
            int yOld = MouseInfo.getPointerInfo().getLocation().y;

            try {
                Thread.sleep(60000); // Check every 60 seconds
            } catch (InterruptedException ex) {
                client.writeToServerLog(Log.LEVEL_ERROR, "[IdleManager] Failed to sleep in the thread\n%s", ex);
            }
            
            int xNew = MouseInfo.getPointerInfo().getLocation().x;
            int yNew = MouseInfo.getPointerInfo().getLocation().y;

            net.packet.Idle packet = new net.packet.Idle();

            packet.idling = (xOld == xNew && yOld == yNew);

            client.sendUDP(packet);
        }
    }

}
